package cn.ainit.base.myTask;

import org.springframework.stereotype.Repository;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OracleTypes;
import cn.ainit.sys.common.dao.BusinessBaseDao;
import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.query.entity.QueryJson;

@Repository
public class MyTaskDaoImpl extends BusinessBaseDao implements MyTaskDao {
	@Override
	public QueryJson findTask(TaskParam taskParam) throws Exception {
		QueryJson queryJson = new QueryJson();
		CallableStatement callStmt = null;
		Connection conn = null;
		String putValue="";
		String whereStr ="";
		String sqlText ="";
		try {
			//格式化sql
//			String sqlStore = "select d.sql_text from sys_datastore d where d.ds_id='"+taskParam.getDsId().split(";")[0]+"'";
//			String dataStSql = this.execSqlForString(sqlStore);
//			sqlText = this.formatDataSql(taskParam.getDsId(),dataStSql,taskParam.getUid());
			List rowsCol = new ArrayList(); 
			conn = this.getJdbcTemplate().getDataSource().getConnection();
			String callName = "{call PK_MYTASK.up_findTask(?,?,?,?,?)}"; //执行存储过程
			callStmt = conn.prepareCall(callName);
			callStmt.setString(1, taskParam.getDsId());
			callStmt.setString(2, taskParam.getUid());
			callStmt.setString(3, taskParam.getStaId()); //站点Id
			callStmt.setString(4, taskParam.getFlagState()); //任务类型标志位
//			callStmt.setString(5, sqlText);
			callStmt.registerOutParameter(5, OracleTypes.VARCHAR);
			callStmt.execute();
			putValue = callStmt.getString(5);
			//目前返回的这个为一个sql
			//执行这个sql
			whereStr = this.configForSql(taskParam.getConfig(),taskParam.getDsId());
			if (whereStr !=null) {
				putValue +=" "+whereStr;
			}
			System.out.println("我的工作台查询按钮执行的sql语句------------"+putValue);
			rowsCol = this.jdbcTemplate.queryForList(putValue);
			queryJson.setRows(rowsCol);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (callStmt != null) {
				callStmt.close();
			}
			if(conn != null){
				conn.close();
			}
		}
		
		return queryJson;
	}
	@Override
	public String getDsIdByType(String djType) throws Exception {
		String dsId = "";
		try {
			String sql = "select c.ds_id from BS_CW_DJTYPE c where c.id='"+djType+"'";
			dsId = this.jdbcTemplate.queryForObject(sql,String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsId;
	}
	
	@Override
	public String getFuncIdByType(String djType) throws Exception {
		String dsId = "";
		try {
			String sql = "select c.func_id from BS_CW_DJTYPE c where c.id='"+djType+"'";
			dsId = this.jdbcTemplate.queryForObject(sql,String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dsId;
	}
	@Override
	public List myWorkList(String uid) throws Exception {
		List list = new ArrayList();
		try {
			String sql = "select m.* from sys_mywork_table m where exists (select 1 from sys_role_user sru where sru.user_id = '" +uid+
			"' and sru.role_id = '1') " +
			"or exists(select 1 from sys_role_right sr where sr.role_id in (select sru.role_id from sys_role_user sru where sru.User_Id='"+uid+
			"') and sr.rtype='2module' and sr.ssid = 'menu_10' and sr.moduleid in ('yw_task','cw_task','sp_task'))"+
			"or exists (select 1 from   sys_role_right srr where  srr.role_id in (select sru.role_id from   sys_role_user sru where  sru.user_id = '"+uid+
			"') and srr.rtype='3gridpagebts' and srr.ssid = 'menu_10' and srr.btid = m.work_id) ORDER  BY m.sortnum";
			list = this.jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List myWorkNum(String uid) throws Exception {
		List list = new ArrayList();
		try {
			String sql = "select m.*,PK_MYTASK.Uf_sqlcount(M.DS_ID,'"+uid+
			"','',M.WORK_CODE) COUNTNUM from sys_mywork_table m where exists (select 1 from sys_role_user sru where sru.user_id = '" +uid+
			"' and sru.role_id = '1') " +
			"or exists(select 1 from sys_role_right sr where sr.role_id in (select sru.role_id from sys_role_user sru where sru.User_Id='"+uid+
			"') and sr.rtype='2module' and sr.ssid = 'menu_10' and sr.moduleid in ('yw_task','cw_task','sp_task'))"+
			"or exists (select 1 from   sys_role_right srr where  srr.role_id in (select sru.role_id from   sys_role_user sru where  sru.user_id = '"+uid+
			"') and srr.rtype='3gridpagebts' and srr.ssid = 'menu_10' and srr.btid = m.work_id) ORDER  BY m.sortnum";
			list = this.jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
